package top.z.orm.handle.osql.core;

import top.z.orm.core.osql.BeeSql;
import top.z.orm.core.osql.Condition;
import top.z.orm.core.osql.ObjToSQL;
import top.z.orm.core.osql.Suid;

import java.util.List;

/**
 * 通过对象来操作数据库, 并返回结果
 */
public class ObjSQL implements Suid {

    private BeeSql   beeSql;// = BeeFactory.getHoneyFactory().getBeeSql();
    private ObjToSQL objToSQL;// = BeeFactory.getHoneyFactory().getObjToSQL();

    public ObjSQL() {
    }

    public BeeSql getBeeSql() {
        if (beeSql == null) {
            beeSql = BeeFactory.getHoneyFactory().getBeeSql();
        }
        return beeSql;
    }

    public void setBeeSql(BeeSql beeSql) {
        this.beeSql = beeSql;
    }

    public ObjToSQL getObjToSQL() {
        if (objToSQL == null) {
            objToSQL = BeeFactory.getHoneyFactory().getObjToSQL();
        }
        return objToSQL;
    }

    public void setObjToSQL(ObjToSQL objToSQL) {
        this.objToSQL = objToSQL;
    }

    @Override
    public <T> List<T> select(T entity) {
        if (entity == null) {
            return null;
        }
        String sql = getObjToSQL().toSelectSQL(entity);
        Logger.logSQL("select sql: ", sql);
        // 返回值用到泛型
        return getBeeSql().select(sql, entity);
    }

    @Override
    public <T> Integer update(T entity) {
        // 当ID为NULL时抛出异常, 在转SQL时抛出
        if (entity == null) {
            return -1;
        }
        String sql = getObjToSQL().toUpdateSQL(entity);
        Logger.logSQL("update sql: ", sql);
        _regEntityClass(entity);
        return getBeeSql().modify(sql);
    }

    @Override
    public <T> Integer insert(T entity) {
        if (entity == null) {
            return -1;
        }
        String sql = getObjToSQL().toInsertSQL(entity);
        Logger.logSQL("insert sql: ", sql);
        _regEntityClass(entity);
        return getBeeSql().modify(sql);
    }

    @Override
    public Integer delete(Object entity) {
        if (entity == null) {
            return -1;
        }
        String sql = getObjToSQL().toDeleteSQL(entity);
        Logger.logSQL("delete sql: ", sql);
        _regEntityClass(entity);
        return getBeeSql().modify(sql);
    }

    @Override
    public <T> List<T> select(T entity, Condition condition) {
        if (entity == null) {
            return null;
        }
        String sql = getObjToSQL().toSelectSQL(entity, condition);
        Logger.logSQL("select sql: ", sql);
        return getBeeSql().select(sql, entity);
    }

    @Override
    public <T> Integer delete(T entity, Condition condition) {
        if (entity == null) {
            return -1;
        }
        String sql = getObjToSQL().toDeleteSQL(entity, condition);
        Logger.logSQL("delete sql: ", sql);
        _regEntityClass(entity);
        return getBeeSql().modify(sql);
    }

    @Override
    public Suid setDynamicParameter(String para, String value) {
        OneTimeParameter.setAttribute(para, value);
        return this;
    }

    private <T> void _regEntityClass(T entity) {
        HoneyContext.regEntityClass(entity.getClass());
    }

    @Override
    public void beginSameConnection() {
        OneTimeParameter.setAttribute("_SYS_Bee_SAME_CONN_BEGIN");
    }

    @Override
    public void endSameConnection() {
        HoneyContext.endSameConnection();
    }
}